package cn.jinjimi.app.main.fragment;

import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;

import cn.jinjimi.app.R;
import cn.jinjimi.app.main.CateTabPresenter;
import cn.jinjimi.app.main.ICategoryView;
import cn.jinjimi.app.main.adapter.FragmentPageAdapter;
import cn.jinjimi.app.main.extend.BaseNetControlFragment;
import cn.jinjimi.domain.entity.ProductCategory;

/**
 * @author zhenhua
 * @date 2018/5/10
 */

public class CateTabFragment extends BaseNetControlFragment
    implements ICategoryView {

    private ListView mCateListNav;
    private CateTabPresenter mPresenter;
    private ViewPager mViewPager;
    private ProductCategoryListAdapter mAdapter;
    private FragmentPageAdapter mFragmentAdapter;

    @Override
    protected int getBizLayoutResourceId() {
        return R.layout.fragment_cate_tab;
    }

    @Override
    protected void initialBiz() {
        View v = findView();
        if (v != null) {
            mCateListNav = v.findViewById(R.id.cate_nav);
            mViewPager = v.findViewById(R.id.vp_fragments);
        }
        mPresenter = new CateTabPresenter(this);
    }

    @Override
    protected void loadData() {
        mPresenter.reqPageCategory();
    }

    @Override
    public void render(List<ProductCategory> list) {
        if (mAdapter == null) {
            mAdapter = new ProductCategoryListAdapter(list);
            mCateListNav.setAdapter(mAdapter);
            mCateListNav.setDividerHeight(0);
            mFragmentAdapter = new FragmentPageAdapter(getChildFragmentManager(), getFragments(list));
            mViewPager.setAdapter(mFragmentAdapter);
        }
        netLoadSucc();
    }

    @Override
    public void render(ProductCategory productCategory) {

    }

    private List<Fragment> getFragments(List<ProductCategory> list) {
        List<Fragment> fragments = new ArrayList<>();
        if (list != null && list.size() > 0) {
            for (ProductCategory category : list) {
                if (TextUtils.equals(category.type, "product")) {
                    ProductCategoryFragment fragment = new ProductCategoryFragment();
                    fragment.setProductCategory(category);
                    fragments.add(fragment);
                } else if (TextUtils.equals(category.type, "country")) {
                    ProductCountryFragment fragment = new ProductCountryFragment();
                    fragment.setProductCategory(category);
                    fragments.add(fragment);
                } else if (TextUtils.equals(category.type, "brand")) {
                    ProductBrandFragment fragment = new ProductBrandFragment();
                    fragment.setProductCategory(category);
                    fragments.add(fragment);
                }
            }
        }
        return fragments;
    }

    public void onItemSelected(int i) {
        if (mViewPager != null) {
            mViewPager.setCurrentItem(i, false);
        }
    }


    /**
     * 导航适配器
     */
    class ProductCategoryListAdapter extends BaseAdapter {
        private final List<ProductCategory> mData;
        private int mCurrentPosition = 0;

        ProductCategoryListAdapter(List<ProductCategory> list) {
            mData = new ArrayList<>(list);
        }

        @Override
        public int getCount() {
            return mData.size();
        }

        @Override
        public ProductCategory getItem(int i) {
            return mData.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(final int position, View convertView, final ViewGroup parent) {
            ViewHolder holder = null;
            if (convertView == null) {
                convertView = LayoutInflater.from(getActivity()).inflate(R.layout.view_label, parent, false);
                holder = new ViewHolder(convertView);
                convertView.setTag(holder);
            }
            if (holder == null) {
                holder = (ViewHolder) convertView.getTag();
            }
            holder.render(getItem(position), mCurrentPosition == position);
            holder.mLabel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mCurrentPosition = position;
                    onItemSelected(position);
                    notifyDataSetChanged();
                }
            });
            return convertView;
        }
    }

    class ViewHolder {
        private Button mLabel;
        private ImageView mImage;

        ViewHolder(View v) {
            mLabel = v.findViewById(R.id.label);
            mImage = v.findViewById(R.id.bg);
        }

        void render(ProductCategory category, boolean selected) {
            if (category != null) {
                mLabel.setText(category.label);
                mImage.setSelected(selected);
                if (selected) {
                    mLabel.setTextColor(ContextCompat.getColor(getActivity(), R.color.black_181818));
                } else {
                    mLabel.setTextColor(ContextCompat.getColor(getActivity(), R.color.text_grey));
                }
            }
        }
    }
}
